**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/inv_sup_attend.log, text replace
set more off

local cutoff = 50
local seed = 9999 
local case_lb = `cutoff'

local covarsmin d_mo_* d_dist_*
local covars vic_female_any vic_white_any acc_white acc_male acc_old cat_3 cat_4 d_mo_* d_dist_* arrests_pre complaints_pre force_pre any_prior_complaint tenure  
 
 
*****************
**BRING IN CASE STATUS + COMPLAINTS DATA AND APPLY DATA RESTRICTION
***************** 
 
do ${rep_root}/code/data_restrict.do

*****************
**MERGE WITH OUTCOMES DATA
***************** 

merge 1:1 cr_id acc_id using ${rep_root}/data/outcomes, keep(3) nogen 

**drop if accused are not police officers
//drop if acc_rank != "POLICE OFFICER"

*****************
**DEFINE TREATMENT AND MAKE COVARIATES
***************** 


**Drop investigators without enough cases
tab inv_id
sort inv_id 
by inv_id: gen inv_ct = _N
drop if inv_ct<`case_lb'
tab inv_id intake_yr

**Define some covariates
gen po = acc_rank == "POLICE OFFICER"
gen minor_inj = vic_inj_1<=1
gen vic_old = (year(inc_complaint_dt_1) - vic_byr_oldest)>=35 & !missing(vic_byr_oldest)
gen acc_old = acc_age>=35 & !missing(acc_age)
gen cat_3 = acc_cat==3
gen cat_4 = acc_cat==4
 
   
  

forvalues i = 1/25{
	gen d_dist_`i' = inc_district_1==`i'
}
forvalues yr = 2006/2008{
	gen d_yr_`yr' = intake_yr==`yr'

	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
	
}
forvalues yr = 2014/2018{
	gen d_yr_`yr' = intake_yr==`yr'
	
	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
}

gen d_noaff = finding_cd=="NO AFFIDAVIT"
gen d_unfound = finding_cd=="UNFOUNDED"
gen d_nosus = finding_cd=="NOT SUSTAINED"
gen d_exon = finding_cd=="EXONERATED"
gen d_sus = finding_cd=="SUSTAINED"
gen d_addlinv = finding_cd=="ADDITIONAL INVESTIGATION REQUESTED"

qui foreach var in vic_female_any vic_white_any acc_white acc_male acc_old cat_3 cat_4 complaints_pre complaints_pre force_pre any_prior_complaint tenure complaints_postc1_3 serious_postc1_3 complaints_postc1_3 merit_postc1_3 d_noaff d_unfound d_nosus d_exon d_sus d_addlinv{
	sum `var'
	*local temp_m = r(mean)
	local ss_`var'_m : di %6.3f r(mean)
	*local temp_sd = r(sd)
	local ss_`var'_sd : di %6.3f r(sd)
}

**Define treatment
 

gen treat_affidavit = finding_cd != "NO AFFIDAVIT"    
gen treat_sustain = finding_cd == "SUSTAINED"   

 


foreach t in treat_affidavit  treat_sustain{

**Create LOM
capture drop resid cr_resid cr_ct inv_tot_resid case inv_ct2  
qui reg `t' `covars'
predict resid, residuals

sort cr_id
by cr_id: egen cr_resid = total(resid)
by cr_id: gen cr_ct = _N

sort inv_id intake_yr intake_mo
gen case = 1
by inv_id intake_yr: egen inv_tot_resid = total(resid)
by inv_id intake_yr: egen inv_ct2 = total(case)
gen lom_`t' = (inv_tot_resid - cr_resid)/(inv_ct2-cr_ct)

 


sort inv_id
by inv_id: sum lom_`t'
}



*****************
**STORE FIRST STAGE RESULTS
***************** 
 
 
**First stage
foreach t in treat_affidavit treat_sustain{

sum `t'
local first_mean = r(mean)

reg `t' lom_`t' `covars', cluster(inv_id)
local first = _b[lom_`t']
local first_se = _se[lom_`t']
local first_N = e(N)
boottest lom_`t', weight(webb) nograph seed(`seed')
local first_f = r(t)^2
local first_p = r(p)

local first : di %6.3f `first'
local first_f : di %6.3f `first_f'

local first_mean_`t' : di %6.3f `first_mean'
local first_`t' : di %6.3f `first'
local first_se_`t' : di %6.3f `first_se'
local first_p_`t' : di %6.3f `first_p'
glo first_star_`t' = cond(`first_p_`t'' <0.01, "***", cond(`first_p_`t'' <0.05, "**", cond(`first_p_`t'' <0.1, "*", "")))
local first_f_`t' : di %6.3f `first_f'
local first_N_`t' : di %6.0f `first_N'
 
}
 
keep if (intake_yr>2008) | (intake_yr == 2008 & intake_mo>=9) //restrict to cases starting in 2008 because attendance data only starts in 2008 

/*

**Standardize LOMs

sum lom_treat_affidavit, d
replace lom_treat_affidavit = lom_treat_affidavit/ `r(sd)'

sum lom_treat_sustain, d
replace lom_treat_sustain = lom_treat_sustain/ `r(sd)'

*/

  
**************************
**REDUCED FORM RESULTS FOR ATTENDANCE & ASSIGNMENT
**************************
local treatvar  treat_affidavit 
 


//INITIATE TABLE TO STORE RESULTS

texdoc init ${rep_root}/output/tables/inv_sup_attend.tex, replace force
tex \begin{tabular}{l*{7}{c}}
tex \hline\hline
tex                     &\multicolumn{1}{c}{Complaints} &\multicolumn{1}{c}{Arrests} &\multicolumn{1}{c}{Type 1 Arrests}&\multicolumn{1}{c}{Attendance}&\multicolumn{1}{c}{No Car}&\multicolumn{1}{c}{District}&\multicolumn{1}{c}{Special} \\
 

texdoc close

 
 
	
local varlist complaints_postc1_3 arrests_postc1_3 attend_postc1_3 type1_arrests_postc1_3 type1_arrests_postc4_12 type1_arrests_posty2  desk_postc1_3 district_postc1_3 special_postc1_3 complaints_postc4_12 arrests_postc4_12  attend_postc4_12 desk_postc4_12 district_postc4_12 special_postc4_12 complaints_posty2 arrests_posty2 attend_posty2 desk_posty2 district_posty2 special_posty2
  
  
**Create Tables

**SECOND STAGE
foreach var of local varlist{

	reg `var' lom_`treatvar' lom_treat_sustain `covars' , cluster(inv_id)
 
	local beta4`var' = _b[lom_`treatvar']
 	
	boottest lom_`treatvar', weight(webb)  nograph seed(`seed')
/*

 
 ivreg `var' `covars' lom_treat_sustain (`treatvar'=lom_`treatvar'), cluster(inv_id)
 	local beta4`var' = _b[`treatvar']
 boottest `treatvar', weight(webb)  nograph seed(`seed')
*/
local p`var' = r(p)
display r(p)


//get stars based on bootstrapped p values 
glo star4_`var' = cond(`p`var'' <0.01, "***", cond(`p`var'' <0.05, "**", cond(`p`var'' <0.1, "*", "")))
local p4`var' : di %6.3f `p`var'' 

matrix b`var' = e(b)
matrix v`var' = e(V)
scalar N= e(N)
local N`var' = N


local se4`var' = sqrt(v`var'[1,1])

local beta4`var' : di %6.3f `beta4`var''
local se4`var' : di %6.3f `se4`var''
local N`var' : di %7.0fc `N`var''



sum `var'
local mean_`var' =r(mean)
local mean_`var' : di %6.2f `mean_`var''
 
 
  
	 
 
**************************
**OUTPUT OLS RESULTS
**************************

texdoc init ${rep_root}/output/tables/inv_sup_attend.tex, append force  
tex \hline 
tex $<$ Month 3  &  `beta4complaints_postc1_3'${star4_complaints_postc1_3}&  `beta4arrests_postc1_3'${star4_arrests_postc1_3}&  `beta4type1_arrests_postc1_3'${star4_type1_arrests_postc1_3}  & `beta4attend_postc1_3'${star4_attend_postc1_3}& `beta4desk_postc1_3'${star4_desk_postc1_3} & `beta4district_postc1_3'${star4_district_postc1_3}& `beta4special_postc1_3'${star4_special_postc1_3} \\
tex          &(`se4complaints_postc1_3')    &(`se4arrests_postc1_3')  &(`se4type1_arrests_postc1_3')   &(`se4attend_postc1_3')& (`se4desk_postc1_3')& (`se4district_postc1_3')& (`se4special_postc1_3')   \\
tex   &[`p4complaints_postc1_3']  &[`p4arrests_postc1_3']  &[`p4type1_arrests_postc1_3'] & [`p4attend_postc1_3']& [`p4desk_postc1_3']& [`p4district_postc1_3']& [`p4special_postc1_3']    \\
tex Dep. var. mean & `mean_complaints_postc1_3' & `mean_arrests_postc1_3'& `mean_type1_arrests_postc1_3' &`mean_attend_postc1_3'& `mean_desk_postc1_3'&`mean_district_postc1_3'& `mean_special_postc1_3' \\
tex Observations & `Ncomplaints_postc1_3'& `Narrests_postc1_3' & `Ntype1_arrests_postc1_3'&`Nattend_postc1_3'& `Ndesk_postc1_3'&`Ndistrict_postc1_3'& `Nspecial_postc1_3' \\
tex \hline 
tex Months 4-12  &  `beta4complaints_postc4_12'${star4_complaints_postc4_12} &  `beta4arrests_postc4_12'${star4_arrests_postc4_12}&  `beta4type1_arrests_postc4_12'${star4_type1_arrests_postc4_12}& `beta4attend_postc4_12'${star4_attend_postc4_12}& `beta4desk_postc4_12'${star4_desk_postc4_12} & `beta4district_postc4_12'${star4_district_postc4_12}& `beta4special_postc4_12'${star4_special_postc4_12} \\
tex          &(`se4complaints_postc4_12')  &(`se4arrests_postc4_12')  &(`se4type1_arrests_postc4_12')  & (`se4attend_postc4_12')& (`se4desk_postc4_12')& (`se4district_postc4_12')& (`se4special_postc4_12')   \\
tex   &[`p4complaints_postc4_12'] &[`p4arrests_postc4_12']&[`p4type1_arrests_postc4_12'] & [`p4attend_postc4_12']& [`p4desk_postc4_12']& [`p4district_postc4_12']& [`p4special_postc4_12']    \\
tex Dep. var. mean & `mean_complaints_postc4_12' & `mean_arrests_postc4_12'& `mean_type1_arrests_postc4_12'&`mean_attend_postc4_12'& `mean_desk_postc4_12'&`mean_district_postc4_12'& `mean_special_postc4_12' \\
tex Observations & `Ncomplaints_postc4_12'& `Narrests_postc4_12'& `Ntype1_arrests_postc4_12' &`Nattend_postc4_12'& `Ndesk_postc4_12'&`Ndistrict_postc4_12'& `Nspecial_postc4_12' \\
tex \hline 
tex Year 2  &  `beta4complaints_posty2'${star4_complaints_posty2} &  `beta4arrests_posty2'${star4_arrests_posty2}&  `beta4type1_arrests_posty2'${star4_type1_arrests_posty2}& `beta4attend_posty2'${star4_attend_posty2}& `beta4desk_posty2'${star4_desk_posty2} & `beta4district_posty2'${star4_district_posty2}& `beta4special_posty2'${star4_special_posty2} \\
tex          &(`se4complaints_posty2')  &(`se4arrests_posty2') &(`se4type1_arrests_posty2')& (`se4attend_posty2')& (`se4desk_posty2')& (`se4district_posty2')& (`se4special_posty2')   \\
tex   &[`p4complaints_posty2'] &[`p4arrests_posty2']&[`p4type1_arrests_posty2'] & [`p4attend_posty2']& [`p4desk_posty2']& [`p4district_posty2']& [`p4special_posty2']    \\
tex Dep. var. mean & `mean_complaints_posty2'& `mean_arrests_posty2' & `mean_type1_arrests_posty2'&`mean_attend_posty2'& `mean_desk_posty2'&`mean_district_posty2'& `mean_special_posty2' \\
tex Observations & `Ncomplaints_posty2' & `Narrests_posty2'  & `Ntype1_arrests_posty2'&`Nattend_posty2'& `Ndesk_posty2'&`Ndistrict_posty2'& `Nspecial_posty2' \\
 
texdoc close


 
 
 

texdoc init ${rep_root}/output/tables/inv_sup_attend.tex, append force   
tex \hline\hline
tex \end{tabular}

texdoc close

 
}







**************************
**Revision: controlling for attendance data and removing desk duty officers
**************************

local treatvar   treat_affidavit  
 


//INITIATE TABLE TO STORE RESULTS

texdoc init ${rep_root}/output/tables/inv_sup_attend_control.tex, replace force
tex \begin{tabular}{l*{3}{c}}
tex \hline\hline
tex                     &\multicolumn{1}{c}{Complaints} &\multicolumn{1}{c}{Arrests} &\multicolumn{1}{c}{Type 1 Arrests}  \\
 

texdoc close

 
 
	
local varlist complaints_postc1_3 arrests_postc1_3 type1_arrests_postc1_3  

 
sum desk_postc1_3,d
  
**Create Tables




**Create Tables

**SECOND STAGE
foreach var of local varlist{

	reg `var' lom_`treatvar' lom_treat_sustain `covars'  , cluster(inv_id)
 
	local beta4`var' = _b[lom_`treatvar']
 	
	boottest lom_`treatvar', weight(webb)  nograph seed(`seed')
/*

 
 ivreg `var' `covars' lom_treat_sustain (`treatvar'=lom_`treatvar'), cluster(inv_id)
 	local beta4`var' = _b[`treatvar']
 boottest `treatvar', weight(webb)  nograph seed(`seed')
*/
local p`var' = r(p)
display r(p)


//get stars based on bootstrapped p values 
glo star4_`var' = cond(`p`var'' <0.01, "***", cond(`p`var'' <0.05, "**", cond(`p`var'' <0.1, "*", "")))
local p4`var' : di %6.3f `p`var'' 

matrix b`var' = e(b)
matrix v`var' = e(V)
scalar N= e(N)
local N`var' = N


local se4`var' = sqrt(v`var'[1,1])

local beta4`var' : di %6.3f `beta4`var''
local se4`var' : di %6.3f `se4`var''
local N`var' : di %7.0fc `N`var''



sum `var'
local mean_`var' =r(mean)
local mean_`var' : di %6.2f `mean_`var''
 
 
  
	
}

 
**************************
**OUTPUT OLS RESULTS
**************************

texdoc init ${rep_root}/output/tables/inv_sup_attend_control.tex, append force  
tex \hline 
tex Without Controls  &  `beta4complaints_postc1_3'${star4_complaints_postc1_3}&  `beta4arrests_postc1_3'${star4_arrests_postc1_3}&  `beta4type1_arrests_postc1_3'${star4_type1_arrests_postc1_3}  \\
tex          &(`se4complaints_postc1_3')    &(`se4arrests_postc1_3')  &(`se4type1_arrests_postc1_3')     \\
tex   &[`p4complaints_postc1_3']  &[`p4arrests_postc1_3']  &[`p4type1_arrests_postc1_3']     \\
tex Dep. var. mean & `mean_complaints_postc1_3' & `mean_arrests_postc1_3'& `mean_type1_arrests_postc1_3'   \\
tex Observations & `Ncomplaints_postc1_3'& `Narrests_postc1_3' & `Ntype1_arrests_postc1_3'  \\
tex \hline 
 
 
texdoc close




**SECOND STAGE
foreach var of local varlist{

	reg `var' lom_`treatvar' lom_treat_sustain `covars' attend_postc1_3 desk_postc1_3 district_postc1_3 special_postc1_3, cluster(inv_id)
 
	local beta4`var' = _b[lom_`treatvar']
 	
	boottest lom_`treatvar', weight(webb)  nograph seed(`seed')
/*

 
 ivreg `var' `covars' lom_treat_sustain (`treatvar'=lom_`treatvar'), cluster(inv_id)
 	local beta4`var' = _b[`treatvar']
 boottest `treatvar', weight(webb)  nograph seed(`seed')
*/
local p`var' = r(p)
display r(p)


//get stars based on bootstrapped p values 
glo star4_`var' = cond(`p`var'' <0.01, "***", cond(`p`var'' <0.05, "**", cond(`p`var'' <0.1, "*", "")))
local p4`var' : di %6.3f `p`var'' 

matrix b`var' = e(b)
matrix v`var' = e(V)
scalar N= e(N)
local N`var' = N


local se4`var' = sqrt(v`var'[1,1])

local beta4`var' : di %6.3f `beta4`var''
local se4`var' : di %6.3f `se4`var''
local N`var' : di %7.0fc `N`var''



sum `var'
local mean_`var' =r(mean)
local mean_`var' : di %6.2f `mean_`var''
 
 
  
	
}

 
**************************
**OUTPUT OLS RESULTS
**************************

texdoc init ${rep_root}/output/tables/inv_sup_attend_control.tex, append force  
tex Controlling for Assignment  &  `beta4complaints_postc1_3'${star4_complaints_postc1_3}&  `beta4arrests_postc1_3'${star4_arrests_postc1_3}&  `beta4type1_arrests_postc1_3'${star4_type1_arrests_postc1_3}  \\
tex          &(`se4complaints_postc1_3')    &(`se4arrests_postc1_3')  &(`se4type1_arrests_postc1_3')     \\
tex   &[`p4complaints_postc1_3']  &[`p4arrests_postc1_3']  &[`p4type1_arrests_postc1_3']     \\
tex Dep. var. mean & `mean_complaints_postc1_3' & `mean_arrests_postc1_3'& `mean_type1_arrests_postc1_3'   \\
tex Observations & `Ncomplaints_postc1_3'& `Narrests_postc1_3' & `Ntype1_arrests_postc1_3'  \\
tex \hline 
 
 
texdoc close

preserve 


**Create Tables
drop if desk_postc1_3>.5 & desk_postc1_3!=.
**SECOND STAGE
foreach var of local varlist{

	reg `var' lom_`treatvar' lom_treat_sustain `covars'  , cluster(inv_id)
 
	local beta4`var' = _b[lom_`treatvar']
 	
	boottest lom_`treatvar', weight(webb)  nograph seed(`seed')
/*

 
 ivreg `var' `covars' lom_treat_sustain (`treatvar'=lom_`treatvar'), cluster(inv_id)
 	local beta4`var' = _b[`treatvar']
 boottest `treatvar', weight(webb)  nograph seed(`seed')
*/
local p`var' = r(p)
display r(p)


//get stars based on bootstrapped p values 
glo star4_`var' = cond(`p`var'' <0.01, "***", cond(`p`var'' <0.05, "**", cond(`p`var'' <0.1, "*", "")))
local p4`var' : di %6.3f `p`var'' 

matrix b`var' = e(b)
matrix v`var' = e(V)
scalar N= e(N)
local N`var' = N


local se4`var' = sqrt(v`var'[1,1])

local beta4`var' : di %6.3f `beta4`var''
local se4`var' : di %6.3f `se4`var''
local N`var' : di %7.0fc `N`var''



sum `var'
local mean_`var' =r(mean)
local mean_`var' : di %6.2f `mean_`var''
 
 
  
	
}

 restore
**************************
**OUTPUT OLS RESULTS
**************************

texdoc init ${rep_root}/output/tables/inv_sup_attend_control.tex, append force  
tex Dropping Desk-Duty Officers  &  `beta4complaints_postc1_3'${star4_complaints_postc1_3}&  `beta4arrests_postc1_3'${star4_arrests_postc1_3}&  `beta4type1_arrests_postc1_3'${star4_type1_arrests_postc1_3}  \\
tex          &(`se4complaints_postc1_3')    &(`se4arrests_postc1_3')  &(`se4type1_arrests_postc1_3')     \\
tex   &[`p4complaints_postc1_3']  &[`p4arrests_postc1_3']  &[`p4type1_arrests_postc1_3']     \\
tex Dep. var. mean & `mean_complaints_postc1_3' & `mean_arrests_postc1_3'& `mean_type1_arrests_postc1_3'   \\
tex Observations & `Ncomplaints_postc1_3'& `Narrests_postc1_3' & `Ntype1_arrests_postc1_3'   \\
 
 
 
 
texdoc close


 
 

texdoc init ${rep_root}/output/tables/inv_sup_attend_control.tex, append force   
tex \hline\hline
tex \end{tabular}

texdoc close

  











  
log close
*clear
